package com.jackrain.nea.fc.cp.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.Set;

/**
 * HR与FC人员接口-04.职务
 * Mapper
 *
 * @author WSL
 * @create 2017/12/15
 */
@Mapper
public interface ViewHrJobMapper {

    /**
     * 判断FC中表【CP_C_JOB】是否存在HR005_ID=pk_job
     * @param job
     * @return JSONObject
     */
    @Select("SELECT COUNT(1) AS COUNT,ID FROM CP_C_JOB WHERE HR005_ID = #{job}")
    JSONObject checkEmp(String job);

    class HrJobInsertSql
    {
        public String insertHrJob(final JSONObject jsonObject)
        {
            return new SQL()
            {
                {
                    INSERT_INTO("cp_c_job");
                    Set<String> keySet =  jsonObject.keySet();
                    for (String key: keySet) {
                        VALUES(key,"#{"+key+"}");
                    }
                }
            } .toString();
        }
    }
    /**
     * 新增
     * @param jsonObject
     */
    @InsertProvider(type = HrJobInsertSql.class,
            method = "insertHrJob")
    void insert(JSONObject jsonObject);

    class HrJobUpdateSql
    {
        public String updateHrJob(final JSONObject jsonObject)
        {
            return new SQL()
            {
                {
                    UPDATE("cp_c_job");
                    Set<String> keySet =  jsonObject.keySet();
                    for (String key: keySet) {
                        if (!"ID".equals(key)) {
                            SET(key + "=#{" + key + "}");
                        }
                    }
                    WHERE("ID=#{ID}");
                }
            } .toString();
        }
    }
    /**
     * 更新
     * @param jsonObject
     * @return int
     */
    @UpdateProvider(type = HrJobUpdateSql.class,
            method = "updateHrJob")
    int update(JSONObject jsonObject);
}
